//调用共用方法

yx.public.navFn()

yx.public.lazyLoadImg()

yx.public.backPageTop()

//banner部分的轮播图
var bannerCarousel = new Carousel()
bannerCarousel.init({
	id: 'bannerPic',   
	autoplay: true,   
	intervalTime: 3000,   
	loop: true,   
	totalNum: 5,   
	moveNum: 1,  
	circle: true,  
	moveWay: 'opacity'  
})

//新品首发部分的轮播图
var newProductCarousel = new Carousel()
newProductCarousel.init({
	id: 'newProduct',   
	autoplay: false,   
	intervalTime: 3000,   
	loop: false,   
	totalNum: 8,   
	moveNum: 4,  
	circle: false,  
	moveWay: 'position'  
})
newProductCarousel.on('leftEnd', function(){
	this.prevBtn.style.background = "#E7E2D7"
})
newProductCarousel.on('rightEnd', function(){
	this.nextBtn.style.background = "#E7E2D7"
})
newProductCarousel.on('leftBtnClick', function(){
	this.nextBtn.style.background = "#b19e7a"
})
newProductCarousel.on('rightBtnClick', function(){
	this.prevBtn.style.background = "#b19e7a"
})


//大家都在说部分 轮播图
var sayCarousel = new Carousel()
sayCarousel.init({
	id: 'sayPic',   
	autoplay: false,   
	intervalTime: 3000,   
	loop: false,   
	totalNum: 6,   
	moveNum: 3,  
	circle: false,  
	moveWay: 'position'  
})

//人气推荐 选项卡效果
;(function(){
	var items = yx.ga("#recommend header li")
	var contents = yx.ga("#recommend .content")
	for(var i=0; i<items.length; i++){
		items[i].index = i
		items[i].onclick = function(){
			for(var i=0; i<items.length; i++){
				items[i].className = ""
				contents[i].style.display = "none"
			}
			items[this.index].className = "active"
			contents[this.index].style.display = "block"
		}
	}
})();


//限时购 
;(function(){
	var timeBox = yx.g("#limit .timeBox")
	var spans = yx.ga("#limit .timeBox span")
	
	showTime()
	var timer = setInterval(showTime, 1000)
	
	//1 倒计时
	function showTime(){
		var endTime = new Date(2120,8,23,21)
	
		if(new Date() < endTime){ //如果当前的时间没有超过结束的时间才去做倒计时
			var overTime = yx.cutTime(endTime)
			spans[0].innerHTML = yx.format(overTime.h)
			spans[1].innerHTML = yx.format(overTime.m)
			spans[2].innerHTML = yx.format(overTime.s)
		}else{
			clearInterval(timer)
		}
	}

	//2 渲染商品数据(和图片懒加载功能 冲突了，先注释掉！！！)
	/* var boxWrap = yx.g("#limit .boxWrap")
	var str = ''
	var items = json_promotion.itemList
	
	for(var i=0; i<items.length; i++){
		str += '<div class="limitBox">' +
						'<a href="#" class="left scaleImg">' +
							'<img class="original" src="images/empty.gif" data-src="'+ items[i].primaryPicUrl +'" >' +	
						'</a>' +
						'<div class="right">' +
							'<a href="#" class="title">'+ items[i].itemName +'</a>' +
							'<p>'+ items[i].simpleDesc +'</p>' +
							'<div class="numBar clearfix">' +
								'<div class="numCon"><span style="width:' + 
								items[i].currentSellVolume/items[i].totalSellVolume*100 +'%"></span></div>' +
								'<span class="numTips">还剩'+ items[i].currentSellVolume +'</span>' +
							'</div>' +
							'<div>' +
								'<span class="xianshi">限时价</span><span class="fuhao">￥</span> <strong>'+ items[i].actualPrice +
								'</strong><span class="yuan">原价' + 
								items[i].retailPrice +'</span>' +
							'</div>' +
							'<a href="#" class="qianggou">立即抢购</a>' +
						'</div>' +
					'</div>'
	}
	boxWrap.innerHTML = str */
})();


//购物车功能
yx.public.cartFn()

//页面跳转功能 
//首页 -> 详情页 点击商品图片，跳转到详情页
;(function(){
	var imgs = yx.ga("img")
	var goodsIdList = [1143021, 1092014, 1113019]  //商品id
	
	imgs.forEach((item) => {
		item.onclick = function(){
			//1 产生一个随机数 [0,3]
			var index = Math.floor(Math.random()*4)
			
			//2 页面跳转
			if(index === 0){
				//如果随机数是0,则跳转到404页面
				window.location.href = './404.html'
			}else{
				//其它
				window.location.href = `./product.html?id=${goodsIdList[index-1]}`
			}
		}
	})
})()


